﻿using Biz.Buffer;
using Biz.EF.Platform.SG;
using Biz.Process.Entity;
using Hjt212Parser;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Biz.Process
{
    public static class Processor2031Sum2Month
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(Processor2031Sum2Month));
        public static void Process(string content)
        {
            
            try
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrWhiteSpace(mn)) return;
                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (!datatime.HasValue) return;
                var port = CommonInformation.GetLetportByMn(mn);
                if (port == null) return;

                using (var pCtx = new PlatformXpfContext())
                {
                    var codes = Parser.GetPollutantCodes(content);
                    if (codes == null) return;
                    Master master = new Master();
                    master.enterpriseId = port.企业基本信息ID;
                    master.letportId = port.ID;
                    DateTime time = datatime.Value;
                    master.monitorTime = time;
                    //todo:主污染物可以在此增加
                    if (codes.Contains("B01") || codes.Contains("w00000"))
                    {
                        master.contamTypeId = 2;
                        var rtd = Parser.GetDoubleByKey("B01-Cou", content);
                        if (!rtd.HasValue) Parser.GetDoubleByKey("w00000-Cou", content);
                        master.sum = rtd ?? 0;
                    }
                    else if (codes.Contains("B02") || codes.Contains("a00000"))
                    {
                        master.contamTypeId = 1;
                        var rtd = Parser.GetDoubleByKey("B02-Cou", content);
                        if (!rtd.HasValue) Parser.GetDoubleByKey("a00000-Cou", content);
                        master.sum = rtd ?? 0;
                    }
                    master.rtd = 0;
                    var begin = new DateTime(time.Year, time.Month, 1, 0, 0, 0);
                    var end = begin.AddMonths(1);

                    var masterRecord = pCtx
                          .主月数据
                          .Where(t => t.企业基本信息ID == port.企业基本信息ID)
                          .Where(t => t.企业排口ID == port.ID)
                          .Where(t => t.监测时间 >= begin)
                          .FirstOrDefault(t => t.监测时间 < end);
                    if (masterRecord == null)
                    {
                        masterRecord = new 主月数据();
                        masterRecord.企业基本信息ID = port.企业基本信息ID;
                        masterRecord.企业排口ID = port.ID;
                        pCtx.主月数据.Add(masterRecord);
                    }
                    masterRecord.监测时间 = master.monitorTime;
                    masterRecord.污染物类别ID = master.contamTypeId;
                    var masterDaysInMonth = pCtx
                        .主日数据
                        .Where(t => t.企业基本信息ID == port.企业基本信息ID)
                        .Where(t => t.企业排口ID == port.ID)
                        .Where(t => t.监测时间 >= begin)
                        .Where(t => t.监测时间 < end)
                        .ToList();
                    masterRecord.时段量 = masterDaysInMonth.Count > 1 ? masterDaysInMonth.Where(t => t.时段量 != null).Sum(t => t.时段量) : 0;
                    pCtx.SaveChanges();
                    var ids = masterDaysInMonth.Select(t => t.ID).ToList();

                    Helper.RemoveMasterFactor(codes, true);

                    foreach (var code in codes)
                    {
                        var contam = CommonInformation.GetContamCode(code);
                        if (contam == null) continue;

                        var slaveRecord = pCtx
                            .从月数据
                            .Where(t => t.主月数据ID == masterRecord.ID)
                            .FirstOrDefault(t => t.污染物编码ID == contam.ID);

                        if (slaveRecord == null)
                        {
                            slaveRecord = new 从月数据();
                            slaveRecord.主月数据ID = masterRecord.ID;
                            slaveRecord.污染物编码ID = contam.ID;

                            pCtx.从月数据.Add(slaveRecord);
                        }

                        var slaveDaysInMonth = pCtx
                            .从日数据
                            .Where(t => ids.Contains(t.ID))
                            .Where(t => t.污染物编码ID == contam.ID).ToList();

                        slaveRecord.Cou = slaveDaysInMonth.Count > 0 ? slaveDaysInMonth.Sum(t => t.Cou) : 0;
                        var list = slaveDaysInMonth.Where(t=>t.ZSCou!=null).ToList();
                        slaveRecord.ZSCou = list.Count > 0 ? list.Sum(t => t.ZSCou) : null;
                        list = slaveDaysInMonth.Where(t => t.Avg != null).ToList();
                        slaveRecord.Avg = list.Count > 0 ? list.Average(t => t.Avg) : null;
                        list = slaveDaysInMonth.Where(t => t.ZSAvg != null).ToList();
                        slaveRecord.ZSAvg = list.Count > 0 ? list.Average(t => t.ZSAvg) : null;

                        list = slaveDaysInMonth.Where(t => t.Min != null).ToList();
                        slaveRecord.Min = list.Count > 0 ? list.Min(t => t.Min) : null;
                        list = slaveDaysInMonth.Where(t => t.ZSMin != null).ToList();
                        slaveRecord.ZSMin = list.Count > 0 ? list.Min(t => t.ZSMin) : null;

                        list = slaveDaysInMonth.Where(t => t.Max != null).ToList();
                        slaveRecord.Max = list.Count > 0 ? list.Max(t => t.Max) : null;
                        list = slaveDaysInMonth.Where(t => t.ZSMax != null).ToList();
                        slaveRecord.ZSMax = list.Count > 0 ? list.Max(t => t.ZSMax) : null;

                        pCtx.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                Log.Error(content);
            }
        }
    }
}
